{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "810b3f51",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import CDIndexOption\n",
    "from gs_quant.risk import DollarPrice, CDDelta, CDVega, CDTheta, CDGamma\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import PricingContext, HistoricalPricingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a01bc09d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "075f0ab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cd_option = CDIndexOption(index_family='iTraxx Europe XOVER', strike=0.0300, option_type='Call', \n",
    "                          buy_sell='Sell', expiration_date='1m', termination_date='5y', notional_amount=1)\n",
    "cd_option.as_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c392b31c",
   "metadata": {},
   "source": [
    "Index Option specific risk measures are:\n",
    "- CDDelta - amount of underlying index in local notional to buy in order to hedge the change in Dollar Price due to a 1bp shift in the underlying index spread\n",
    "- CDGamma - amount of underlying index in local notional to buy in order to hedge the change in option delta due to a 1bp shift in the underlying index spread\n",
    "- CDVega - change in option Dollar Price due to a 1bp shift in the implied volatility of the underlying index \n",
    "- CDTheta - change in option Dollar Price over one day, assuming constant vol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b9e49d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "with PricingContext(market_data_location='LDN'):\n",
    "    greeks = cd_option.calc((CDDelta, CDGamma, CDVega, CDTheta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a42c52b",
   "metadata": {},
   "outputs": [],
   "source": [
    "greeks.result()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9508a54e",
   "metadata": {},
   "source": [
    "View historic delta:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71dd4b37",
   "metadata": {},
   "outputs": [],
   "source": [
    "with HistoricalPricingContext(date(2021, 5, 5), date(2021, 5, 11), show_progress=True, is_batch=True):\n",
    "    previous_option_deltas_holder = cd_option.calc(CDDelta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2752b5d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "previous_option_deltas = previous_option_deltas_holder.result()\n",
    "print(previous_option_deltas.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edcb96c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot the instrument delta over time\n",
    "import matplotlib.pyplot as plt\n",
    "from pandas.plotting import register_matplotlib_converters\n",
    "register_matplotlib_converters()\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(previous_option_deltas.value)\n",
    "ax.set_xlabel('time')\n",
    "ax.set_ylabel('delta')\n",
    "ax.set_title('delta over time')\n",
    "fig.autofmt_xdate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
